package main

import (
	"Project/global"
	"Project/model/common"
	"Project/service"
	"Project/utils"
	"fmt"
	"go.uber.org/zap"
	"strings"
	"time"
)
import "github.com/robfig/cron"

// 定时任务
func cronJob() {
	c := cron.New()
	// 每天统计药店 购药记录条数
	c.AddFunc("59 59 23 * * *", func() {
		SendEmail("day")
	})
	// 每周统计药店 购药记录条数
	c.AddFunc("59 59 23 * * 0", func() {
		SendEmail("week")
	})
	c.Start()
}

var serviceDrug = service.ServiceGroupApp.DrugServiceGroup.DrugRecordService
var serviceSuper = service.ServiceGroupApp.SystemServiceGroup.UserSuperService

// 设置邮件内容
func setEmailContent(tp string) (string, error) {
	var content string
	content = ` <!DOCTYPE html>
	<html lang="en">
	<head>
	<meta charset="iso-8859-15">
	<title>MMOGA POWER</title>
	</head>
	<body>
	<h1>药品登记系统:药店登记情况统计结果</h1>
	?
	</body>
	</html>`
	year, month, day := time.Now().Date() // 获取当天的日期
	var data string
	if tp == "day" {
		start := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
		end := time.Date(year, month, day, 23, 59, 59, 999, time.Local)
		// 获取当天的登记记录
		res, err := getResultByTime(start, end)
		data = fmt.Sprintf("TIME: %d-%d-%d\n\r<table>\n\r<tr><td>药店名</td><td>登记次数</td></tr>\n\r", year, month, day)
		if err != nil {
			return "", err
		}
		for _, v := range res {
			data += fmt.Sprintf("<tr><td>%s</td><td>%d</td></tr>\n\r", v.Name, v.Count_num)
		}
		data += fmt.Sprintf("</table>\n\r")
		// 获取至今没有登记的药店
		name, err := getSince()
		if err != nil {
			return "", err
		}
		data += fmt.Sprintf("至今未登记的药店：%s", strings.Join(name, "、"))

	} else { // week
		// 本周药店登记情况
		start := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
		start = start.Add(-24 * 6 * time.Hour)
		end := time.Date(year, month, day, 23, 59, 59, 999, time.Local)
		res, err := getResultByTime(start, end)
		syear, smonth, sday := start.Date()
		data = fmt.Sprintf("TIME: %d-%d-%d ~ %d-%d-%d \n\r<table>\n\r<tr><td>药店名</td><td>登记次数</td></tr>\n\r", syear, smonth, sday, year, month, day)
		if err != nil {
			return "", err
		}
		for _, v := range res {
			data += fmt.Sprintf("<tr><td>%s</td><td>%d</td></tr>\n\r", v.Name, v.Count_num)
		}
		data += fmt.Sprintf("</table>\n\r")
		// 至今未登记的药店
		name, err := getSince()
		if err != nil {
			return "", err
		}
		data += fmt.Sprintf("至今未登记的药店：%s\n\r", strings.Join(name, "、"))
	}
	content = strings.Replace(content, "?", data, -1)
	return content, nil
}

// 获取统计结果 时间范围
func getResultByTime(start, end time.Time) ([]common.StatisticRes, error) {
	StatisRes, err := serviceDrug.GetResByTime(start, end)
	if err != nil {
		global.GVA_LOG.Error("获取统计记录失败", zap.Error(err))
		return nil, err
	}
	return StatisRes, nil
}

// 获取统计结果 至今
func getSince() ([]string, error) {
	shopName, err := serviceDrug.GetResSince()
	if err != nil {
		global.GVA_LOG.Error("获取统计记录失败", zap.Error(err))
		return nil, err
	}
	return shopName, nil
}

// 获取所有收件人邮箱
func getToUserEmail() ([]string, error) {
	emails, err := serviceSuper.UserEmails()
	if err != nil {
		global.GVA_LOG.Error("获取超级用户邮箱错误", zap.Error(err))
		return nil, err
	}
	return emails, nil
}

// SendEmail 发送邮件
func SendEmail(tp string) {
	email := global.GVA_CONFIG.Email
	content, _ := setEmailContent(tp)
	to, _ := getToUserEmail()
	err := utils.SendToMail(email.From, email.Nickname, email.Secret, email.Host+":"+email.Port, "统计结果", content, "html", to)
	if err != nil {
		global.GVA_LOG.Error("发送邮件失败", zap.Error(err))
		return
	}
}
